﻿/*
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Routing;
using Scaffold.Views;
namespace Scaffold
{
    

    public class ScaffoldApplication
    {
        public static void Initialize(Type dataContextType, string masterPageFile, string contentPlaceHolderID)
        {

            InitializeMvc(masterPageFile, contentPlaceHolderID);
            DataContextType = dataContextType;
            Type listViewPageofT = typeof(Scaffold.Views.ListPageBase<>);
            Type stubViewPageOfT = typeof(Scaffold.Views.StubViewPage<>);

            Entries =
            (
                from p in dataContextType.GetProperties()
                where p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(System.Data.Linq.Table<>)
                let itemType = p.PropertyType.GetGenericArguments()[0]
                select new ScaffoldEntry()
                {
                    Name = p.Name,
                    ItemType = itemType,
                    Views = new Dictionary<string, Scaffold.Views.ViewPageInfo>()
                        {
                            { "List", new ViewPageInfo(listViewPageofT.MakeGenericType(itemType), itemType.Name, string.Format("~/Views/{0}/List.aspx", itemType.Name), masterPageFile, contentPlaceHolderID)},
                            { "View", new ViewPageInfo(stubViewPageOfT.MakeGenericType(itemType), itemType.Name, string.Format("~/Views/{0}/View.aspx", itemType.Name), masterPageFile, contentPlaceHolderID)},
                            { "Edit", new ViewPageInfo(stubViewPageOfT.MakeGenericType(itemType), itemType.Name, string.Format("~/Views/{0}/Edit.aspx", itemType.Name), masterPageFile, contentPlaceHolderID)}
                        }
                }
            ).ToList();
        }

        private static void InitializeMvc(string masterPageFile, string contentPlaceHolderID)
        {
            // Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
            //       automatic support on IIS6 and IIS7 classic mode


            RouteTable.Routes.Add(new Route("{controller}/{action}/{id}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { action = "Index", id = "" }),
            });

            RouteTable.Routes.Add(new Route("Default.aspx", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),
            });

            ControllerBuilder.Current.SetControllerFactory(new ScaffoldControllerFactory());
            MasterPage = masterPageFile;
            ContentPlaceHolderID = contentPlaceHolderID;
        }
        public static Type DataContextType { get; set; }
        public static List<ScaffoldEntry> Entries { get; protected set; }
        public static string MasterPage { get; protected set; }
        public static string ContentPlaceHolderID { get; protected set; }
        public static ScaffoldEntry FindEntity(string controllerName)
        {
            return (from e in Entries
                       where e.ItemType.Name  == controllerName
                       select e).FirstOrDefault();
        }



    }
}
